#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 100005
int n, m;
vector<vector<int>> vertexMat;
queue<int> que;
bool discover[maxn];
enum
{
    unvisited,
    isvisited
};
void dfs(int x)
{
    cout << x << " ";
    discover[x] = isvisited;
    for (int i = 0, to; i < vertexMat[x].size(); ++i)
    {
        to = vertexMat[x][i];
        if (discover[to] == unvisited)
            dfs(to);
    }
}
void init(bool needsort = true)
{
    for (int i = 1; i <= n; ++i)
    {
        discover[i] = unvisited;
        if (needsort)
            sort(vertexMat[i].begin(), vertexMat[i].end());
    }
}
void bfs(int x)
{
    que.push(x);
    while (!que.empty())
    {
        int node = que.front();
        que.pop();
        discover[node] = isvisited;
        for (int i = 0, to; i < vertexMat[node].size(); ++i)
        {
            to = vertexMat[node][i];
            if (discover[to] == unvisited)
                que.push(to);
        }
        cout << node << " ";
    }
}
int main()
{
    cin >> n >> m; //n个顶点,m条边
    vertexMat.resize(n + 1);
    for (int i = 0, frm, to; i < m; ++i)
    {
        cin >> frm >> to;
        vertexMat[frm].push_back(to);
    }
    init();
    dfs(1);
    init(false);
    cout << endl;
    bfs(1);
    return 0;
}